﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Markup;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using System.IO;
using MapSurface.AssemblyReflexion;


namespace MapSurface.Engine
{
    public class ModelManager
    {

        private UIModelElement.UIModelManager _uimodelManager;
        private ChoiceManager _choiceWidget;

        public UIModelElement.UIModelManager UimodelManager
        {
            get { return _uimodelManager; }
            set { _uimodelManager = value; }
        }
        private WidgetSurface.XamlObjectManager _xamlManager;

        public WidgetSurface.XamlObjectManager XamlManager
        {
            get { return _xamlManager; }
            set { _xamlManager = value; }
        }
        private string dllfile = "";

        public ModelManager(String uiModelFile, String choiceFile, System.Windows.Controls.Canvas mainCanvas, String apiFile, string dllFile, List<Delegate> selectHanlder)
        {
            if (mainCanvas.Children.Count > 0)
                mainCanvas.Children.Clear();
            this.dllfile = dllFile;
            try
            {
                _choiceWidget = new ChoiceManager(choiceFile, dllFile, selectHanlder);
            }
            catch (Exception)
            {
                _choiceWidget = null;
            }
            _uimodelManager = new MapSurface.UIModelElement.UIModelManager(uiModelFile, dllfile);
            _xamlManager = new MapSurface.WidgetSurface.XamlObjectManager(mainCanvas, dllfile, apiFile,choiceFile, selectHanlder);
            
            //Setter Handlers
            _uimodelManager.UIModelLoadHappened += _xamlManager.UiModelLoaderHandler;

            _xamlManager.ReplaceUIElementHappened += _uimodelManager.ReplaceUIElement;
            _xamlManager.DeleteUIElementHappened += _uimodelManager.DeleteUIElementHandler;
            _xamlManager.CheckDeleteHappened += _uimodelManager.CheckUIModel;
            _uimodelManager.CheckBeforeDelete += _xamlManager.ModelMessageHandler;
        }

        public void loadUIModel()
        {
            _uimodelManager.doLoadModel();
        }
        public void deleteXamlElement(object instance)
        {
            _xamlManager.deleteXamlObject(instance);
        }
        public void replace(ref object root, object newIntance, object instance, bool all)
        {
            _xamlManager.replaceObject(ref root, newIntance, instance, all);
        }

        public static List<SurfaceListBoxItem> loadFile(string dirName)
        {
            List<SurfaceListBoxItem> ret = new List<SurfaceListBoxItem>();
            try
            {
                string[] filePaths = Directory.GetFiles(dirName);
                foreach (string file in filePaths)
                {
                    if (file.EndsWith(".xmi"))
                    {
                        SurfaceListBoxItem item = new SurfaceListBoxItem();

                        string[] items = file.Split('\\');
                        item.ContentStringFormat = file;
                        item.Content = items[items.Length - 1];
                        ret.Add(item);
                    }
                }
            }
            catch (Exception)
            {
            }
            return ret;
        }

    }
}
